package 刷题.leedcode142;
//快慢指针法
public class Solution1 {
    public ListNode detectCycle(ListNode head){
        if(head == null || head.next == null){
            return null;
        }
        ListNode prev = head;
        ListNode cur = head;
        while(cur != null && cur.next != null){
                prev = prev.next;
                cur = cur.next.next;
                if(prev == cur){
                    ListNode ptr = head;
                    while(ptr != prev){
                        ptr = ptr.next;
                        prev = prev.next;
                    }
                    return ptr;
            }
        }
        return null;
    }
}
